var canvasCard = require('../../../wxjs/canvasCard.js');
const app = getApp();

var data = {
  xhx_images: app.xhx_images,
};

var currentPage = {
  data,
  form: {}, //详情请求参数
  onLoad,
  loadInit, //跳转到登录授权成功后返回
  getDetail,
  newCanvas, //名片生成
  canvasToTempFilePath, //保存到手机
  onShareAppMessage: function() {
    const that = this;
    const uuid = that.data.uuid;
    return {
      title: '',
      path: app.xhx_route.leagueList + "?uuid=" + uuid
    }
  }
};

var page = Object.assign({}, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  // 团课uuid
  const uuid = options && options.uuid || '';
  // 活动uuid
  const activityUuid = options && options.activityUuid || '';
  that.setData({
    uuid,
    activityUuid,
  })
  that.form.uuid = uuid;
  that.form.activityUuid = activityUuid;
  that.newCanvas();

  app.xlogin.getUser(function(res) {
    if (res) that.loadInit();

    var myInfo = app.isNavigateToLogin();
    if (!myInfo) return;

    myInfo = app.isNavigateToBind();
    if (!myInfo) return;
  })
}

function loadInit() {
  const that = this;
  that.getDetail();
}

function getDetail() {
  const that = this;
  const activityUuid = that.data.activityUuid;
  let temp = {};

  app.middleware.requestAjax({
    serAddr: app.serAddrs[activityUuid ? "getQcForShare" : "getQCode"],
    data: that.form,
    cancelToast: true,
    success: function(res) {
      let xbody = res.data;
      xbody.headPortrait = app.userInfo.headPortrait;
      xbody.sex = app.userInfo.sex;
      xbody.username = app.userInfo.username;
      xbody.createTime = app.middleware.time().typeB;
      if (xbody.qrCodeUrl) {
        app.downloadFile.downloadFile({
          url: xbody.qrCodeUrl,
          success: function(path) {
            xbody.qrCodeUrl = path;

            if (xbody.headPortrait) {
              app.downloadFile.downloadFile({
                url: xbody.headPortrait,
                success: function(path) {
                  xbody.headPortrait = path;
                  temp.xbody = xbody;
                  that.setData(temp);
                  that.newCanvas();
                }
              })
            } else {
              temp.xbody = xbody;
              that.setData(temp);
              that.newCanvas();
            }

          }
        })
      }
    }
  })
}

/**
 * 名片生成
 */
function newCanvas() {
  var that = this;
  var xbody = that.data.xbody || {};

  if (xbody.createTime) {
    let arr = xbody.createTime.split(" ");
    xbody.createTime = arr[0];
  }

  let data = {
    title: xbody.name,
    lable: xbody.label,
    address: xbody.gymName,
    bgImg: app.xhx_images.canvasBg,
    headimg: xbody.headPortrait,
    nickName: xbody.username,
    code: xbody.qrCodeUrl,
    cardDays: xbody.createTime,
  };

  console.log("海报卡片参数", data);

  if (that.myCanvas) {
    that.myCanvas.canvasData(data)
    return;
  }

  that.myCanvas = new canvasCard.canvas({
    canvasId: 'myCanvas',
    data,
    changeSize: function(res) {
      that.setData(res)
      console.log('changeSize', res)
    },
    success: function() {
      that.setData({
        msg: ''
      })
    }
  });
}

/**
 * 保存到手机
 */
function canvasToTempFilePath() {
  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  if (this.myCanvas) this.myCanvas.canvasToTempFilePath();
}